package com.esisa.java.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class FormClient extends JPanel implements ActionListener {
	
	private Form form;
	private JButton first,next,last,presious,close;
	private JTable produit;
	private DefaultTableModel model;
	private JPanel bouttons;
	
	private GestionClient gClient;
	int cp=0;
	
	public FormClient(JFrame owner,GestionClient g) {
		
		//super(owner,true);
		gClient=g;
		
		this.setLayout(new BorderLayout());
		bouttons=new JPanel();
		bouttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		
		model=new DefaultTableModel(new String[]{"code","desin","prix","Qte"},1);
		produit=new JTable(model);
		
		
		first=new JButton("First");
		next=new JButton("Next");
		presious=new JButton("Presious");
		last=new JButton("Last");
		close=new JButton("Close");
		
		first.addActionListener(this);
		next.addActionListener(this);
		presious.addActionListener(this);
		last.addActionListener(this);
		close.addActionListener(this);
		
		form=new Form(new Client());
		
		bouttons.add(first);
		bouttons.add(next);
		bouttons.add(presious);
		bouttons.add(last);
		bouttons.add(close);
		
		add(bouttons,"South");
		add(form,"North");
		add(produit,"West");
		
	}
	public void clear() {
		for (int i = model.getRowCount()-1; i > 0; i--) {
			model.removeRow(i);
		}
	}
	public void SetValue(Client client){
	
		clear();
		form.setValue(0,client.getCode());
		form.setValue(1,client.getNom() );
		form.setValue(2,client.getPrenom());
		for (int i = 0; i <client.getProduits().size(); i++) {
			
			Produit p=new Produit(client.getProduit(i));
			String code=""+p.getCode();
			String prix=""+p.getPrix();
			String qte=""+p.getQuantite();
			model.addRow(new String[]{code,p.getDesignation(),prix,qte});
			
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton b=(JButton)e.getSource();
		switch (b.getText()) {
		case "First":
			cp=0;
			SetValue(gClient.getClient(cp));
			break;
		case "Next":
			cp++;
			if(cp<gClient.size())
			SetValue(gClient.getClient(cp));
			break;
		case "Presious":
			cp--;
			if(cp>=0)
			SetValue(gClient.getClient(cp));
			break;
		case "Last":
			cp=gClient.size()-1;
			SetValue(gClient.getClient(gClient.size()-1));
			break;
		case "Close":
			System.exit(0);
			break;
			
		default:
			break;
		}
		
	}


}
